//
//  PresentViewController.m
//  Version1.0_RectiveCocoa
//
//  Created by 吴可高 on 2018/1/20.
//  Copyright © 2018年 吴可高. All rights reserved.
//

#import "PresentViewController.h"
#import "PhotoTool.h"
#import "AlbumModel.h"
#import "PhotoContainerView.h"
#import "DetailViewController.h"
@interface PresentViewController ()
@property(nonatomic,strong) NSMutableArray * albums;
@property(nonatomic,strong) PhotoContainerView * containerView;
@property(nonatomic,strong) DetailViewController * datePhotoVC;
@end

@implementation PresentViewController
-(DetailViewController *)datePhotoVC{
    _datePhotoVC = ({
        if (!_datePhotoVC) {
            _datePhotoVC = [[DetailViewController alloc]init];
        }
        _datePhotoVC;
    });
    return _datePhotoVC;
}

-(NSMutableArray *)albums{
    _albums = ({
        if (!_albums) {
            _albums = [NSMutableArray array];
        }
        _albums;
    });
    return _albums;
}
-(PhotoContainerView *)containerView{
    _containerView = ({
        if (!_containerView) {
            _containerView = [[PhotoContainerView alloc]init];
        }
        _containerView;
    });
    return _containerView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self fatchSystemAlbumPhotos];
    NSLog(@"self.albums =%@",self.albums);
    __weak  typeof(self)weakSelf = self;
    [self.view addSubview:self.containerView];
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.view).with.offset(88.f); make.left.right.and.bottom.mas_equalTo(weakSelf.view);
    }];
    __strong typeof(weakSelf)strongSelf = weakSelf;
    self.containerView.albums = self.albums;
    self.containerView.completeBlock = ^(NSIndexPath *indexPath, AlbumModel *model) {
        strongSelf.datePhotoVC.albumModel = model;
        [strongSelf presentViewController:strongSelf.datePhotoVC animated:YES completion:nil];
    };
}

-(void)fatchSystemAlbumPhotos{
    //获取智能相册
    PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    [smartAlbums enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([[PhotoTool transFormPhotoTitle:collection.localizedTitle] isEqualToString:@"相机胶卷"] || [[PhotoTool transFormPhotoTitle:collection.localizedTitle] isEqualToString:@"所有照片"]){
            PHFetchOptions *option = [[PHFetchOptions alloc] init];
            option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
            option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
            //获取照片信息
            PHFetchResult * result = [PHAsset fetchAssetsInAssetCollection:collection options:option];
            AlbumModel * albumModel = [[AlbumModel alloc]init];
            albumModel.count = result.count;
            albumModel.albumName = collection.localizedTitle;
            albumModel.result = result;
            albumModel.index = 0;
            [self.albums addObject:albumModel];
        }
    }];
    //获取用户相册
    PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
    [userAlbums enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(PHAssetCollection * collection, NSUInteger idx, BOOL * _Nonnull stop) {
        // 是否按创建时间排序
        PHFetchOptions *option = [[PHFetchOptions alloc] init];
        option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
        option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
        // 获取照片集合
        PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:collection options:option];
        // 过滤掉空相册
        if (result.count > 0) {
            AlbumModel *albumModel = [[AlbumModel alloc] init];
            albumModel.count = result.count;
            albumModel.albumName = [PhotoTool transFormPhotoTitle:collection.localizedTitle];
            albumModel.result = result;
            [self.albums addObject:albumModel];
        }
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
